<template>
<div>
    <div id="top">
        <h4>
           配对主机选择 
        </h4>
        <form role="form">
            <div class="form-group">
                <label>主机状态</label> 
                <select class="form-control" v-model="searchStatus">
                    <option value="">
                        请选择
                    </option>
                    <option value="done">
                        已经配置
                    </option>
                    <option value="todo">
                        等待配置
                    </option>
                </select>
               <label>输入关键字(mac地址) </label>
                <input  class="form-control" placeholder="请输入" v-model="searchKeyWord"> </input>
            </div>
        </form>
        <!-- <form role="form">
            <div class="form-group">
                <label>输入关键字(mac地址) </label>
                <input  class="form-control" placeholder="请输入" v-model="searchKeyWord"> </input>
            </div>
        </form> -->
        <h4>
           已选门店 ：{{selectedProtector ?  selectedProtector.mac + ", " + selectedProtector.ip : "暂未选择" }}
        </h4>
    </div>
    <div id="content">
        <div>
            <table style="width:100%;">
                <thead css="display:block; ">
                    <tr>
                        <th>
                            所属门店
                        </th>
                        <th>
                            主机类型
                        </th>
                        <th>
                            mac地址
                        </th>
                        <th>
                           状态
                        </th>
                        
                    </tr>
                </thead>
                <tbody css="display:block;overflow:auto;">
                    <tr v-for="v in searchResult"  :class="{ active: selectedProtector && v.mac == selectedProtector.mac }" @click="select( v )" >
                        <td>
                            {{v.shopid ? shops[v.shopid].name : '尚未绑定'}}
                        </td>
                        <td>
                            {{v.detail }}
                        </td>
                        <td>
                            {{v.mac}}
                        </td>
                        <td>
                            <span v-if="v.shopid">已配置/{{v.shopid}}</span>
                            <span v-else>未配置</span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div v-if="waitingConfirm" class="confirmPanel">
        <div >
            <p>
                警告： 当前主机已经绑定门店 {{ shops[waitingConfirm].shopname }} , 编号{{ waitingConfirm }}
                <br/>
                从新绑定当前设备， 将会取消之前的绑定 
            </p>
            <p>
                <div class="confirmButton">
                   <button @click="confirmSelect">确定</button>  <span>  </span><button @click="hiddenConfirmPanel">取消</button>
                </div>
            </p>
        </div>
    </div>
    <div id="footer" >
        <router-link tag="button" to="selectShop" class="btn btn-primary" @click.native="$store.commit('setStep',1)"> 上一步</router-link>
        <router-link tag="button" to="configNVR" class="btn btn-primary" @click.native="$store.commit('setStep',3)" :disabled="!selectedProtector"> 下一步</router-link>    
    </div>
</div>
</template>

<style scoped>
.active{
    background-color:#0087da;
}
#top h4:nth-of-type(1){
    font-weight: bold;
    font-size: 20px;
    margin: 50px 0px 30px 30px;
}
#top h4:nth-of-type(2){
    font-size: 16px;
    margin: 0px 0px 30px 30px;
}
#top label{
    margin: 0px 30px 20px 30px;
}
#top .form-control{
    display: inline-block;
    width: 200px;
}
#top label{
    font-size: 18px;
}   


#content{
    margin-left: 30px;
    margin-top: 50px;
    height: 380px;
    width: 800px;
    overflow: auto;
    border: 1px solid #e1e1e1; 
}
#content th{
    width: 200px;
    height: 50px;
    text-align: center;
    border-left: 1px solid #e1e1e1;
    background-color: #f2f2f2;
}
#content td{
    width: 200px;
    height: 50px;
    text-align: center;
    border-left: 1px solid #e1e1e1;
}
#content tbody{
    overflow: auto;
    height: 400px;
}
#footer{
    margin-left: 30px;
    margin-top: 50px;
    width: 800px;
    text-align: center;
}
#footer button{
    margin: 0px 20px;
}

.confirmPanel{
    z-index: 10000;
    background-color:#aaaaaa;
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0; 
}
.confirmPanel>div{
    width: 300px;
    height: 200px;
    border-radius: 20px;
    background-color:white;
    position: absolute;
    top: 50%;
    left: 40%;
    transform: translateX( -400px );
    transform: translateY( -50%);
}
.confirmPanel > div > p { 
    margin-top: 40px;
    margin-bottom: 40px;
}

.confirmPanel .confirmButton{
    position: absolute;
    width: 100%;
    bottom: 0px;
    left: 0px;
}
.confirmPanel button {
    display: inline-block;
    float: left;
    width: 50%;
}


</style>

<script>
import axios from 'axios'
export default { 
    data(){ 
        return {
            searchKeyWord:'',
            searchStatus:'',
            waitingConfirm: null
        }
    },
    methods: {
        select( v ){ 
            if( v.shopid  ){
                this.waitingConfirm = v.shopid;
            }
            else {
                this.$store.commit( "setProtector", v )
            }
        },
        confirmSelect(  ){
            this.$store.commit( "setProtector", this.$store.state.shops[this.waitingConfirm] );
            this.waitingConfirm = null;
        },
        hiddenConfirmPanel(){
            this.waitingConfirm = null;
        } 
    },
    computed: { 
        clientWidth(){
            return window.document.body.clientWidth;
        },
        clientHeight(){
            return window.document.body.clientHeight;
        },
        shops(){
            return this.$store.state.shops;
        },
        protectors() { 
            return this.$store.state.protectors;
        },
        searchResult() {
            var ret = [];
            console.log( "computed: ", this.protectors )
            for( let id in this.protectors ) {
                if( this.protectors[id].mac.search( this.searchKeyWord ) >= 0 ) {
                    if(this.searchStatus == ''){
                        ret.push( this.protectors[id] )
                    }else if(this.searchStatus == 'done' && this.protectors[id].shopid){
                        ret.push( this.protectors[id] )
                    }else if(this.searchStatus == 'todo' && !this.protectors[id].shopid){
                        ret.push( this.protectors[id] )
                    }
                }
            }
            console.log('after :', ret);
            return ret;
        },
        selectedProtector(){
            return this.$store.state.protector;
        }
    },
}
</script>